# views.py
from django.http import JsonResponse
from django.http import HttpResponse
from django.views import View
from serverworker.models import electricity_meter,Temperature_humidity
from datetime import datetime


#电表列表 test
def electricity_meter_list(request):
    meter_list = ['TAB_01', 'TAB_A1', 'TAB_22', 'TAB_Q1', 'TAB_03']
    # 返回格式：每个电表作为一个对象，包含id和name属性
    meters = [{'id': meter, 'name': meter} for meter in meter_list]
    return JsonResponse(meters, safe=False)  # safe=False 允许返回列表
#温度计列表
def temperature_meter_list(request):
    meter_list = ['TMP_w温度计01', 'TMP_w温度计A1','TMP_w温度计A2','TMP_w温度计A3']
    meters = [{'id': meter, 'name': meter} for meter in meter_list]
    return JsonResponse(meters, safe=False)

#数据接口
def ChartDataView_electricity(request):
    if 1:
        start_date = request.GET.get('start_date')
        end_date = request.GET.get('end_date')


        # 查询数据
        data = electricity_meter.objects.filter(
            create_time__gte=start_date,
            create_time__lt=end_date
        ).order_by('create_time')

        u = list(data.values_list('u', flat=True))
        i = list(data.values_list('i', flat=True))
        q = list(data.values_list('q', flat=True))
        w = list(data.values_list('w', flat=True))
        timestamps = list(data.values_list('create_time', flat=True))
        timestamps = [dt.replace(microsecond=0) for dt in timestamps]
        print(u, i, q, w,timestamps)
        return JsonResponse({
            'u': u,
            'i': i,
            'q': q,
            'w': w,
            'labels': timestamps,
        })

def ChartDataView_temperature(request):
    if 1:
        start_date = request.GET.get('start_date')
        end_date = request.GET.get('end_date')

        # 查询数据
        data = Temperature_humidity.objects.filter(
            create_time__gte=start_date,
            create_time__lt=end_date
        ).order_by('create_time')


        temperature = list(data.values_list('temperature', flat=True))
        humidity = list(data.values_list('humidity', flat=True))
        timestamps = list(data.values_list('create_time', flat=True))
        timestamps = [dt.replace(microsecond=0) for dt in timestamps]
        print(temperature, humidity, timestamps)
        return JsonResponse({
            'temperature': temperature,
            'humidity': humidity,
            'labels': timestamps,
        })


def iot_status(request):
    if 1:
        start_date = request.GET.get('start_date')
        end_date = request.GET.get('end_date')

        # 查询数据
        data = Temperature_humidity.objects.filter(
            create_time__gte=start_date,
            create_time__lt=end_date
        ).order_by('create_time')


        temperature = list(data.values_list('temperature', flat=True))
        humidity = list(data.values_list('humidity', flat=True))
        timestamps = list(data.values_list('create_time', flat=True))
        timestamps = [dt.replace(microsecond=0) for dt in timestamps]
        print(temperature, humidity, timestamps)
        return JsonResponse({
            'temperature': temperature,
            'humidity': humidity,
            'labels': timestamps,
        })